# Maintainer: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: loqs
# Contributor: Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: Vesa Kaihlavirta <vesa@archlinux.org>
# Contributor: Sarah Hay <sarahhay@mb.sympatico.ca>
# Contributor: Tom Burdick <thomas.burdick@wrightwoodtech.com>
# Contributor: Ricardo Catalinas Jiménez <jimenezrick@gmail.com>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - configure with --disable-pgo to fix ARM FTBFS

pkgbase=erlang
pkgname=(erlang erlang-unixodbc)
pkgver=23.3.2
pkgrel=1
_docver=23.3
arch=(x86_64)
url='https://www.erlang.org'
license=(Apache)
makedepends=(fop git glu java-environment libxslt lksctp-tools
             mesa perl unixodbc wxgtk3)
options=(staticlibs)
source=("git+https://github.com/erlang/otp.git#tag=OTP-$pkgver"
        "$url/download/otp_doc_man_$_docver.tar.gz"
        wx3.patch
        erlang-22.0-dont-ignore-LDFLAGS.patch
        epmd.conf
        epmd.service
        epmd.socket)
sha256sums=('SKIP'
            'b890e99d3fe1b317ed083455985225550ebf74b4a8ec2af4c758e4ce6e2934ff'
            'b0e1d2ed833feb0f308060c2a451fcd58fceb68c9c63432fab38ae4cd0264ee2'
            'd84d3644621df383ba24d0f4132dfdc92dc175c12a8f82dd0f107c2d0e7fd626'
            '78ce5e67b21758c767d727e56b20502f75dc4385ff9b6c6db312d8e8506f2df2'
            'b121ec9053fb37abca5f910a81c526f93ec30fe13b574a12209223b346886a9e'
            '998a759e4cea4527f9d9b241bf9f32527d7378d63ea40afa38443c6c3ceaea34')

prepare() {
  # For Erlang to find wx-config-gtk3 as wx-config in the $PATH
  mkdir -p bin
  ln -s /usr/bin/wx-config-gtk3 bin/wx-config
  export PATH="$srcdir/bin:$PATH"

  cd otp

  # Patch for autoconf
  git cherry-pick -n c5b04efb55304a7c6f569c33c8e5875a32877f31

  patch -p1 -i ../erlang-22.0-dont-ignore-LDFLAGS.patch
  patch -p1 -i ../wx3.patch

  ./otp_build autoconf
}

build() {
  cd otp
  export PATH="$srcdir/bin:$PATH"
  export WX_GKT_VER="3.0"
  ./configure \
    --prefix=/usr \
    --enable-builtin-zlib \
    --enable-smp-support \
    --with-odbc \
    --with-wx-config=/usr/bin/wx-config-gtk3 \
    --disable-pgo
  make
}

package_erlang() {
  pkgdesc='General-purpose concurrent functional programming language developed by Ericsson'
  depends=(glu ncurses openssl wxgtk3)
  optdepends=('erlang-unixodbc: database support'
              'java-environment: for Java support'
              'lksctp-tools: for SCTP support')
  provides=(erlang-nox)
  conflicts=(erlang-nox)

  export PATH="$srcdir/bin:$PATH"
  make -C otp DESTDIR="$pkgdir" install

  # move files that belong to the erlang-unixodbc package
  mkdir -p unixodbc
  mv "$pkgdir/usr/lib/erlang/lib/odbc"* "$srcdir/unixodbc/"

  # services and configuration
  install -Dm644 epmd.service "$pkgdir/usr/lib/systemd/system/epmd.service"
  install -Dm644 epmd.socket "$pkgdir/usr/lib/systemd/system/epmd.socket"
  install -Dm644 epmd.conf "$pkgdir/etc/conf.d/epmd"

  # readme and licenses
  install -Dm644 otp/README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
  install -Dm644 otp/LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 otp/CONTRIBUTING.md "$pkgdir/usr/share/doc/$pkgname/CONTRIBUTING.md"
  install -Dm644 otp/AUTHORS "$pkgdir/usr/share/doc/$pkgname/AUTHORS"

  # man pages
  cp -r man "$pkgdir/usr/lib/erlang/"
}

package_erlang-unixodbc() {
  pkgdesc='Unixodbc support for Erlang'
  depends=(erlang-nox unixodbc)

  install -d "$pkgdir/usr/lib/erlang/lib"
  mv unixodbc/* "$pkgdir/usr/lib/erlang/lib/"
  install -Dm644 otp/LICENSE.txt \
    "$pkgdir/usr/share/licenses/$pkgname/LICENCE.txt"
}

# getver: raw.githubusercontent.com/erlang/otp/maint/OTP_VERSION
